/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.modeldocument;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import queryexecute.CorbaExecutor;
import dgbgui.modeldocument.primitives.Concept;
import dgbgui.modeldocument.primitives.Layer;
import dgbgui.modeldocument.primitives.RelationType;

public class ModelFactory {
    
    public static Set<DGBModel> getAllModels() {
        Set<DGBModel> models = new LinkedHashSet<DGBModel>();
        DGBModel verySimpleMammalian = getVerySimpleMammalianModel();
        if (verySimpleMammalian != null) {
            models.add(verySimpleMammalian);
        }
        return models;
    }
    
    // TODO: Remove this, only temporary
    public static int getInstanceCount(String typename) {
        Map<String,Integer> instanceCountMap = new HashMap<String,Integer>();
        instanceCountMap.put("go_term",2561);
        instanceCountMap.put("human_gene",46171);
        instanceCountMap.put("mouse_gene",76152);
        return instanceCountMap.get(typename);
    }
    

    private static DGBModel getVerySimpleMammalianModel() {
        
        // ---------- Concepts ----------
        
        // Go terms
        QueryResult result = CorbaExecutor.getSyncronousResult("ct:go_term.name[|go_term|];");
        if ( result.getPrimitives().size() != 1) {
            return null;
        }
        Concept go_term = (Concept)result.getPrimitives().iterator().next();
        //Concept go_term2 = (Concept)go_term.clone();
        
        // Human Genes
        result = CorbaExecutor.getSyncronousResult("ct:human_gene.name[|human_gene|];");
        if ( result.getPrimitives().size() != 1) {
            return null;
        }
        Concept human_gene = (Concept)result.getPrimitives().iterator().next();
        
        // Mouse genes
        result = CorbaExecutor.getSyncronousResult("ct:mouse_gene.name[|mouse_gene|];");
        if ( result.getPrimitives().size() != 1) {
            return null;
        }
        Concept mouse_gene = (Concept)result.getPrimitives().iterator().next();
        
        // ---------- Relation Types ----------
        
        result = CorbaExecutor.getSyncronousResult("rt:describes");
        if ( result.getPrimitives().size() != 1) {
            return null;
        }
        RelationType describes = (RelationType)result.getPrimitives().iterator().next();
        
//        result = CorbaExecutor.getSyncronousResult("rt:go_is_a");
//        if ( result.getPrimitives().size() != 1) {
//            return null;
//        }
//        RelationType goIsA = (RelationType)result.getPrimitives().iterator().next();
        
        // ---------- Layers ----------
        
        result = CorbaExecutor.getSyncronousResult("lt:gene.name[human_genes]");
        if ( result.getPrimitives().size() != 1) {
            return null;
        }
        Layer human_genes = (Layer)result.getPrimitives().iterator().next();
        
        result = CorbaExecutor.getSyncronousResult("lt:gene.name[mouse_genes]");
        if ( result.getPrimitives().size() != 1) {
            return null;
        }
        Layer mouse_genes = (Layer)result.getPrimitives().iterator().next();

        
//        result = CorbaExecutor.getSyncronousResult("lt:gene_ontology.devdate[20:03:2007]");
//        if ( result.getPrimitives().size() != 1) {
//            return null;
//        }
//        Layer gene_ontology = (Layer)result.getPrimitives().iterator().next();
//        
//        result = CorbaExecutor.getSyncronousResult("lt:gene_ontology.devdate[13:04:2007]");
//        if ( result.getPrimitives().size() != 1) {
//            return null;
//        }
//        Layer pheno_ontology = (Layer)result.getPrimitives().iterator().next();

        
        DGBModel model = new DGBModel("Very simple mammalian");
        model.addConcept(go_term);
        model.addConcept(human_gene);
        model.addConcept(mouse_gene);
        
        Concept go_term2 = (Concept)go_term.clone();
        model.addConcept(go_term2);
        
        model.addEdge(go_term2, mouse_gene, describes, mouse_genes);
        model.addEdge(go_term, human_gene, describes, human_genes);
        model.addEdge(go_term, mouse_gene, describes, mouse_genes);
        
        return model;

    }

}
